﻿using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormOption : FormTemplet
    {
        private FolderBrowserDialog folderBrowserDialog1;

        private FormMain frmMain;

        private bool _bChangePic = false;

        private bool _bChangeFormMain = false;

        private bool _bChangeOrderFormStyle = false;

        private bool _bChangeTechnicsFormStyle = false;

        private bool _bChangeLabelStyle = false;

        private bool _bChangeWayOfLogoPrint = false;

        private bool _bChangeVertical = false;

        private bool _bChangeDirection = false;

        private bool _bChangeMirror = false;

        private bool _bChangePrinterName = false;

        private bool _bChangePath = false;

        private FormOption()
        {
            this.InitializeComponent();
        }

        public FormOption(FormMain frmMain) : this()
        {
            this.frmMain = frmMain;
            this.picBrowser.Image = this.frmMain.BackgroundImage;
            if (this.frmMain.FormBorderStyle == FormBorderStyle.None)
            {
                this.ckFullScreen.Checked = true;
            }
            try
            {
                string text = AppSettings.GetAppSettings("OffsetY");
                this.numericUpDownVertical.Value = Convert.ToInt32(text);
                text = AppSettings.GetAppSettings("barcodePrinterName");
                this.txtSetPrinter.Text = text;
                text = AppSettings.GetAppSettings("direction");
                this.ckDirection.Checked = (text == "1");
                text = AppSettings.GetAppSettings("mirror");
                this.ckMirror.Checked = (text == "1");
                text = AppSettings.GetDefaultVersionOfOutType("order");
                if (text == "1")
                {
                    this.cbSelectOrderFormStyle.SelectedIndex = 0;
                }
                else
                {
                    this.cbSelectOrderFormStyle.SelectedIndex = 1;
                }
                text = AppSettings.GetDefaultVersionOfOutType("technicsForm");
                if (text == "1")
                {
                    this.cbSelectTechnicsFormStyle.SelectedIndex = 0;
                }
                else
                {
                    this.cbSelectTechnicsFormStyle.SelectedIndex = 1;
                }
                text = AppSettings.GetAppSettings("barcodePrinterSensor");
                if (text == "0")
                {
                    this.cbSelectLabelStyle.SelectedIndex = 0;
                }
                else
                {
                    this.cbSelectLabelStyle.SelectedIndex = 1;
                }
                text = AppSettings.GetAppSettings("printLogo");
                if (text == "1")
                {
                    this.cbPrintLogo.SelectedIndex = 0;
                }
                else
                {
                    this.cbPrintLogo.SelectedIndex = 1;
                }
                text = AppSettings.GetAppSettings("exportOrderPath");
                this.txtExportOrderPath.Text = text;
                text = AppSettings.GetAppSettings("exportInvoicePath");
                this.txtExportInvoicePath.Text = text;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            this.btnApply.Enabled = false;
            this._bChangePic = false;
            this._bChangeFormMain = false;
            this._bChangeOrderFormStyle = false;
            this._bChangeTechnicsFormStyle = false;
            this._bChangeLabelStyle = false;
            this._bChangeVertical = false;
            this._bChangePrinterName = false;
            this._bChangePath = false;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "图片文件|*.bmp;*.emf;*.exif;*.gif;*.ico;*.jpeg;*.jpg;*.png;*.wmf|All File(*.*)|*.*";
            this.openFileDialog1.Title = "请选择你要输入的路径和文件名";
            this.openFileDialog1.RestoreDirectory = true;
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtPic.Text = this.openFileDialog1.FileName;
                try
                {
                    this.picBrowser.Image = Image.FromFile(this.txtPic.Text);
                    this._bChangePic = true;
                    this.btnApply.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }

        private void lkNonPicture_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.txtPic.Text = "";
            this.picBrowser.Image = null;
            this.btnApply.Enabled = true;
            this._bChangePic = true;
        }

        private void ckFullScreen_CheckedChanged(object sender, EventArgs e)
        {
            this._bChangeFormMain = true;
            this.btnApply.Enabled = true;
        }

        private void cbSelectOrderFormStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._bChangeOrderFormStyle = true;
            this.btnApply.Enabled = true;
        }

        private void cbSelectTechnicsFormStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._bChangeTechnicsFormStyle = true;
            this.btnApply.Enabled = true;
        }

        private void cbSelectLabelStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._bChangeLabelStyle = true;
            this.btnApply.Enabled = true;
        }

        private void cbPrintLogo_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._bChangeWayOfLogoPrint = true;
            this.btnApply.Enabled = true;
        }

        private void numericUpDownVertical_ValueChanged(object sender, EventArgs e)
        {
            this._bChangeVertical = true;
            this.btnApply.Enabled = true;
        }

        private void ckDirection_CheckedChanged(object sender, EventArgs e)
        {
            this._bChangeDirection = true;
            this.btnApply.Enabled = true;
        }

        private void ckMirror_CheckedChanged(object sender, EventArgs e)
        {
            this._bChangeMirror = true;
            this.btnApply.Enabled = true;
        }

        private void txtSetPrinter_TextChanged(object sender, EventArgs e)
        {
            this._bChangePrinterName = true;
            this.btnApply.Enabled = true;
        }

        private void btnExportOrderPath_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.SelectedPath = this.txtExportOrderPath.Text;
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtExportOrderPath.Text = this.folderBrowserDialog1.SelectedPath;
                this._bChangePath = true;
                this.btnApply.Enabled = true;
            }
        }

        private void btnExportInvoicePath_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.SelectedPath = this.txtExportInvoicePath.Text;
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtExportInvoicePath.Text = this.folderBrowserDialog1.SelectedPath;
                this._bChangePath = true;
                this.btnApply.Enabled = true;
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (this._bChangeFormMain)
            {
                if (this.ckFullScreen.Checked)
                {
                    this.frmMain.WindowState = FormWindowState.Minimized;
                    this.frmMain.FormBorderStyle = FormBorderStyle.None;
                    this.frmMain.WindowState = FormWindowState.Maximized;
                }
                else
                {
                    this.frmMain.WindowState = FormWindowState.Minimized;
                    this.frmMain.FormBorderStyle = FormBorderStyle.Sizable;
                    this.frmMain.WindowState = FormWindowState.Maximized;
                }
                this._bChangeFormMain = false;
            }
            if (this._bChangePic)
            {
                try
                {
                    if (this.txtPic.Text != "")
                    {
                        this.frmMain.BackgroundImage = new Bitmap(this.txtPic.Text);
                    }
                    else
                    {
                        this.frmMain.BackgroundImage = null;
                    }
                    AppSettings.SetAppSettings("backgroundPicture", this.txtPic.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
                this._bChangePic = false;
            }
            if (this._bChangeOrderFormStyle)
            {
                try
                {
                    if (this.cbSelectOrderFormStyle.SelectedIndex == 0)
                    {
                        AppSettings.SetDefaultVersionOfOutType("order", "1");
                    }
                    else
                    {
                        AppSettings.SetDefaultVersionOfOutType("order", "4");
                    }
                    this._bChangeOrderFormStyle = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
            }
            if (this._bChangeTechnicsFormStyle)
            {
                try
                {
                    if (this.cbSelectTechnicsFormStyle.SelectedIndex == 0)
                    {
                        AppSettings.SetDefaultVersionOfOutType("technicsForm", "1");
                    }
                    else
                    {
                        AppSettings.SetDefaultVersionOfOutType("technicsForm", "4");
                    }
                    this._bChangeTechnicsFormStyle = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
            }
            if (this._bChangeLabelStyle)
            {
                try
                {
                    AppSettings.SetAppSettings("barcodePrinterSensor", (this.cbSelectLabelStyle.SelectedIndex == 0) ? 0.ToString() : 1.ToString());
                    this._bChangeLabelStyle = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
            }
            if (this._bChangeWayOfLogoPrint)
            {
                try
                {
                    AppSettings.SetAppSettings("printLogo", (this.cbPrintLogo.SelectedIndex == 0) ? "1" : "0");
                    this._bChangeOrderFormStyle = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
            }
            if (this._bChangeVertical)
            {
                try
                {
                    AppSettings.SetAppSettings("OffsetY", this.numericUpDownVertical.Value.ToString());
                    this._bChangeVertical = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
            }
            if (this._bChangeDirection)
            {
                try
                {
                    AppSettings.SetAppSettings("direction", this.ckDirection.Checked ? "1" : "0");
                    this._bChangeDirection = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
            }
            if (this._bChangeMirror)
            {
                try
                {
                    AppSettings.SetAppSettings("mirror", this.ckMirror.Checked ? "1" : "0");
                    this._bChangeMirror = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
            }
            if (this._bChangePrinterName)
            {
                try
                {
                    AppSettings.SetAppSettings("barcodePrinterName", this.txtSetPrinter.Text.Trim());
                    this._bChangePrinterName = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
            }
            if (this._bChangePath)
            {
                try
                {
                    AppSettings.SetAppSettings("exportOrderPath", this.txtExportOrderPath.Text.Trim());
                    AppSettings.SetAppSettings("exportInvoicePath", this.txtExportInvoicePath.Text.Trim());
                    this._bChangePath = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
            this.btnApply.Enabled = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.btnApply_Click(null, null);
            this.frmMain.TopMost = false;
            base.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
